package com.tommy.jiami.ui;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.tommy.jiami.R;
import com.tommy.jiami.utils.Utils;

public class MainActivity extends AppCompatActivity {
    DrawerLayout drawerLayout;
    FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        fragmentManager = getFragmentManager();
        setupStatusBar();

    }

    public void toLogin(View view) {

    }

    public void startPassage(MenuItem item) {
        startFragment("passage", PassageFragment.class, false);
    }

    public void startFile(MenuItem item) {
        startFragment("file", FileFragment.class, false);
    }

    public void startMedia(MenuItem item) {
        startFragment("media", MediaFragment.class, false);
    }

    public void startApp(MenuItem item) {
        startFragment("app", AppFragment.class, false);
    }

    public void startSetting(MenuItem item) {
        startActivity(new Intent(this, SettingsActivity.class));
    }

    public void startAbout(MenuItem item) {
        startActivity(new Intent(this, AboutActivity.class));
    }

    public void startFragment(String tag, Class<? extends Fragment> clazz, boolean addToBackStack) {
        try {
            Fragment mainFragment = fragmentManager.findFragmentByTag(tag);
            if (mainFragment == null)
                mainFragment = clazz.newInstance();
            FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.replace(R.id.main_content, mainFragment, tag);
            if (addToBackStack) ft.addToBackStack(null);
            ft.commit();
            drawerLayout.closeDrawer(Gravity.START);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void setupStatusBar() {
        if (Build.VERSION.SDK_INT >= 21) {
            findViewById(R.id.top_mask).setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.getStatusBarHeight()));
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(Gravity.START)) {
            drawerLayout.closeDrawer(Gravity.START);
            return;
        }
        super.onBackPressed();
    }
}
